package com.lining.orangeservice.entity.abstractentity.impl;

import com.lining.orangeservice.common.enums.OrderStatus;
import com.lining.orangeservice.entity.abstractentity.Order;
import com.lining.orangeservice.entity.po.OrderItem;

import java.math.BigDecimal;
import java.util.stream.Collectors;

/**
 * 普通订单
 */
public class NormalOrder extends Order {
    private static final BigDecimal DISCOUNT_THRESHOLD = new BigDecimal("100");
    private static final BigDecimal DISCOUNT_RATE = new BigDecimal("0.95"); // 95折
    
    @Override
    public void calculateAmount() {
        if (items == null || items.isEmpty()) {
            totalAmount = BigDecimal.ZERO;
            discountAmount = BigDecimal.ZERO;
            actualAmount = BigDecimal.ZERO;
            return;
        }
        
        // 计算总金额
        totalAmount = items.stream()
            .map(OrderItem::getSubTotal)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
        
        // 普通订单：满100元打95折
        if (totalAmount.compareTo(DISCOUNT_THRESHOLD) >= 0) {
            discountAmount = totalAmount.multiply(BigDecimal.ONE.subtract(DISCOUNT_RATE));
        } else {
            discountAmount = BigDecimal.ZERO;
        }
        
        actualAmount = totalAmount.subtract(discountAmount);
    }
    
    @Override
    public boolean canCancel() {
        return status == OrderStatus.PENDING || status == OrderStatus.PAID;
    }
    
    @Override
    public boolean canRefund() {
        return status == OrderStatus.PAID || status == OrderStatus.SHIPPED;
    }
}